/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Pemasukan;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import tools.koneksi;

/**
 *
 * @author w7
 */
public class PemasukanKontrol {

    Connection conn;
    private static PemasukanKontrol pemasukanKontrol;

    public PemasukanKontrol(Connection koneksi) {
        this.conn = koneksi;
    }

    public static PemasukanKontrol getKoneksiPemasukan() throws SQLException {
        koneksi k = new koneksi();
        if (pemasukanKontrol == null) {
            pemasukanKontrol = new PemasukanKontrol(k.getDBConnection());
        }
        return pemasukanKontrol;

    }

    public void tambahDataSimpanan(Pemasukan b) throws SQLException {
        PreparedStatement statement = null;
        try {
            conn.setAutoCommit(false);
            String sql1 = "insert into "
                    + " transaksi (TANGGAL, NO_INVOICE, KETERANGAN, KODE_REKENING) values (?,?,?,?)";
            String sql2 = "insert into "
                    + " PEMASUKAN1 (NO_INVOICE, ID_ANGGOTA, JUMLAH_DANA_MASUK) values (?,?,?)";

            statement = (PreparedStatement) conn.prepareStatement(sql1);
            statement.setDate(1, new Date(b.getTanggal().getTime()));
            statement.setString(2, b.getNoInvoice());
            statement.setString(3, b.getKeterangan());
            statement.setString(4, b.getKodeRekening());
            statement.executeUpdate();
            statement = (PreparedStatement) conn.prepareStatement(sql2);
            statement.setString(1, b.getNoInvoice());
            statement.setString(2, b.getID_Anggota());
            statement.setDouble(3, b.getJumlahDanaMasuk());
            statement.executeUpdate();
            conn.commit();
        } catch (SQLException exception) {
            conn.rollback();
            throw exception;
        } finally {
            try {
                conn.setAutoCommit(true);
                if (statement != null) {
                    statement.close();
                }
            } catch (SQLException exception) {
                throw exception;
            }
        }
    }

    public void tambahDataCicilan(Pemasukan b) throws SQLException {
        PreparedStatement statement = null;
        try {
            conn.setAutoCommit(false);
            String sql1 = "insert into "
                    + " transaksi (TANGGAL, NO_INVOICE, KETERANGAN, KODE_REKENING) values (?,?,?,?)";
            String sql2 = "insert into "
                    + " PEMASUKAN1 (NO_INVOICE, ID_ANGGOTA, JUMLAH_DANA_MASUK, POKOK_PINJAMAN,  BUNGA, LAMA_PINJAM, CICILAN_KE, angsuran) values (?,?,?,?,?,?,?,?)";
            statement = (PreparedStatement) conn.prepareStatement(sql1);
            statement.setDate(1, new Date(b.getTanggal().getTime()));
            statement.setString(2, b.getNoInvoice());
            statement.setString(3, b.getKeterangan());
            statement.setString(4, b.getKodeRekening());
            statement.executeUpdate();
            statement = (PreparedStatement) conn.prepareStatement(sql2);
            statement.setString(1, b.getNoInvoice());
            statement.setString(2, b.getID_Anggota());
            statement.setDouble(3, b.getJumlahDanaMasuk());
            statement.setDouble(4, b.getPokokPinjaman());
           
            statement.setDouble(5, b.getBunga());
            statement.setDouble(6, b.getLamaPinjam());
            statement.setString(7, b.getCicilanKe());
             statement.setDouble(8, b.getAngsuran());
            statement.executeUpdate();
            conn.commit();
        } catch (SQLException exception) {
            conn.rollback();
            throw exception;
        } finally {
            try {
                conn.setAutoCommit(true);
                if (statement != null) {
                    statement.close();
                }
            } catch (SQLException exception) {
                throw exception;
            }
        }
    }

    public Pemasukan getNamaOtomatis(String ID_Anggota) throws SQLException {
        PreparedStatement statement = null;
        ResultSet result = null;
        Pemasukan k = null;
        try {
            conn.setAutoCommit(false);
            statement = conn.prepareStatement("select NAMA_ANGGOTA from ANGGOTA where ID_ANGGOTA=?");
            statement.setString(1, ID_Anggota);
            result = statement.executeQuery();
            while (result.next()) {
                k = new Pemasukan();
                k.setNama_anggota(result.getString(1));

            }
        } catch (SQLException ex) {
            Logger.getLogger(PemasukanKontrol.class.getName()).log(Level.SEVERE, null, ex);
        }
        return k;
    }

    public String getKodeOtomatis() throws SQLException {
        ResultSet rset = null;
        int kode = 0;
        String sql = "select kode_rekening from pemasukan2 order by no_invoice;";
        Statement stat = conn.createStatement();
        rset = stat.executeQuery(sql);
        while (rset.next()) {
            kode = Integer.parseInt(rset.getString(1)) + 1;

        }
        return String.valueOf(kode);
    }
    
   
}
